<template>
    <div>
        <iview-table
                ref="table"
                :data="data"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,changeSelect:changeSelect}">
        </iview-table>
    </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    export default {
        components: {
            iviewTable,
            iviewPage
        },
        props: {
            projectId: {
                type: Number,
                default: -1
            },
            data: {
                type: Array,
                default: []
            }
        },
        data () {
            return {
                baseURL: '/user/treat/plan/product',
                selectRows: [],
                selectRow: {},
                status: '',
                useCount: '',
                queryModel: {
                    // 网关代理路径(必须)+资源路径(可选)
                    baseURL: '/user/treat/plan/product',
                    defaultFilters: [
                        {'field': 'project_id', 'value1': this.projectId, 'operate': '=='},
                    ]
                },
                dataModel: {
                    menuId: -1,
                    columns: [
                        {
                            title: '处方产品',
                            key: 'productName'
                        },
                        {
                            title: '总数量',
                            key: 'quantity',
                            width: 150
                        },
                        {
                            title: '已使用',
                            key: 'execute',
                            width: 150
                        },
                        {
                            title: '本次使用',
                            key: 'use',
                            width: 150,
                            render: (h, params) => {
                                var vm = this
                                return h('InputNumber',{
                                    props: {
                                        max: params.row.quantity - params.row.execute
                                    },
                                    style: {
                                        width: '100%',
                                        margin: '5px 0px 5px 0px'
                                    },
                                    on: {
                                        'on-change' (value) {
                                        }
                                    }
                                });
                            }
                        }
                    ],
                    order: 'id desc'
                },
                pageModel: {
                    totalSize: 0,
                    pageNumber: 1,
                    pageSize: 10,
                    pageSizeOpts: [10, 50, 100, 500]
                },
                showModal: false
            };
        },
        methods: {
            save (row) {
                let url = this.baseURL + '/save'
                this.$ajax.post(url, row).then((response) => {
                    let {code, msg} = response.data
                    if (code === 0) {
                        this.$bus.alert('success', '已保存')
                    } else {
                        this.$bus.alert('error', msg)
                    }
                })
            },
            refresh () {
                if (this.projectId !== -1) {
                    this.queryModel.defaultFilters[0] = {'field': 'project_id', 'value1': this.projectId, 'operate': '=='}
                }
                this.$refs.table.refresh()
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            },
            init () {
                this.refresh()
            }
        },
        mounted () {
            // this.init()
        }
    }
</script>

<style>

</style>
